import 'dart:io';
import 'package:qr_code_app/generated/json/base/json_convert_content.dart';
import 'package:todo_flutter/todo_flutter.dart';

class ApiConvert extends BaseConvert {
  @override
  BaseEntity<T> convert<T>(Result result) {
    if (result.statusCode == HttpStatus.ok) {
      final data = result.data?['data'];

      ///分页数据
      if (data is Map && data.containsKey('datas')) {
        throw UnknownException('ApiConvert 分页数据未实现');
      } else {
        return BaseEntity(
          code: result.data?['code'],
          message: result.data?['message'],
          data: JsonConvert.fromJsonAsT(data),
        );
      }
    } else {
      ///网络请求 code != HttpStatus.ok
      return BaseEntity(
        code: result.statusCode ?? BaseEntity.defaultCode,
        message: result.statusMessage,
      );
    }
  }
}
